/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package G3.Mod;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author Dymion
 */
public class fsconvers extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        retrieveFS fs = new retrieveFS();
        setContentView(R.layout.fsconversion);

        Spinner systemSpinner = (Spinner) findViewById(R.id.system_spinner);
        Spinner cacheSpinner = (Spinner) findViewById(R.id.cache_spinner);
        Spinner dataSpinner = (Spinner) findViewById(R.id.data_spinner);
        Spinner sdSpinner = (Spinner) findViewById(R.id.sd_spinner);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.extension_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        systemSpinner.setAdapter(adapter);
        cacheSpinner.setAdapter(adapter);
        dataSpinner.setAdapter(adapter);
        sdSpinner.setAdapter(adapter);

        TextView systemErr = (TextView) findViewById(R.id.systemErr);
        TextView dataErr = (TextView) findViewById(R.id.dataErr);
        TextView cachErr = (TextView) findViewById(R.id.cacheErr);
        TextView sdErr = (TextView) findViewById(R.id.sdErr);

        //all the checks for the system partition
        if (adapter.getItem(0).equals(fs.retrieveSystem().replaceAll(" ", ""))) {
            systemSpinner.setSelection(0);
            systemErr.setText("Current fs: " + adapter.getItem(0));
        } else if (adapter.getItem(1).equals(fs.retrieveSystem().replaceAll(" ", ""))) {
            systemSpinner.setSelection(1);
            systemErr.setText("Current fs: " + adapter.getItem(1));
        } else if (adapter.getItem(2).equals(fs.retrieveSystem().replaceAll(" ", ""))) {
            systemSpinner.setSelection(2);
            systemErr.setText("Current fs: " + adapter.getItem(2));
        } else if (adapter.getItem(3).equals(fs.retrieveSystem().replaceAll(" ", ""))) {
            systemSpinner.setSelection(3);
            systemErr.setText("Current fs: " + adapter.getItem(3));
        } else {
            systemErr.setText("The current fs is not supported by this app.");
        }

        //all the checks for the data partition
        if (adapter.getItem(0).equals(fs.retrieveData().replaceAll(" ", ""))) {
            dataSpinner.setSelection(0);
            dataErr.setText("Current fs: " + adapter.getItem(0));
        } else if (adapter.getItem(1).equals(fs.retrieveData().replaceAll(" ", ""))) {
            dataSpinner.setSelection(1);
            dataErr.setText("Current fs: " + adapter.getItem(1));
        } else if (adapter.getItem(2).equals(fs.retrieveData().replaceAll(" ", ""))) {
            dataSpinner.setSelection(2);
            dataErr.setText("Current fs: " + adapter.getItem(2));
        } else if (adapter.getItem(3).equals(fs.retrieveData().replaceAll(" ", ""))) {
            dataSpinner.setSelection(3);
            dataErr.setText("Current fs: " + adapter.getItem(3));
        } else {
            dataErr.setText("The current fs is not supported by this app.");
        }

        //all the checks for the cache partition
        if (adapter.getItem(0).equals(fs.retrieveCache().replaceAll(" ", ""))) {
            cacheSpinner.setSelection(0);
            cachErr.setText("Current fs: " + adapter.getItem(0));
        } else if (adapter.getItem(1).equals(fs.retrieveCache().replaceAll(" ", ""))) {
            cacheSpinner.setSelection(1);
            cachErr.setText("Current fs: " + adapter.getItem(1));
        } else if (adapter.getItem(2).equals(fs.retrieveCache().replaceAll(" ", ""))) {
            cacheSpinner.setSelection(2);
            cachErr.setText("Current fs: " + adapter.getItem(2));
        } else if (adapter.getItem(3).equals(fs.retrieveCache().replaceAll(" ", ""))) {
            cacheSpinner.setSelection(3);
            cachErr.setText("Current fs: " + adapter.getItem(3));
        } else {
            cachErr.setText("The current fs is not supported by this app.");
        }

        //all the checks for the sdcard partition
        if (adapter.getItem(0).equals(fs.retrieveSD().replaceAll(" ", ""))) {
            sdSpinner.setSelection(0);
            sdErr.setText("Current fs: " + adapter.getItem(1));
        } else if (adapter.getItem(1).equals(fs.retrieveSD().replaceAll(" ", ""))) {
            sdSpinner.setSelection(1);
            sdErr.setText("Current fs: " + adapter.getItem(1));
        } else if (adapter.getItem(2).equals(fs.retrieveSD().replaceAll(" ", ""))) {
            sdSpinner.setSelection(2);
            sdErr.setText("Current fs: " + adapter.getItem(1));
        } else if (adapter.getItem(3).equals(fs.retrieveSD().replaceAll(" ", ""))) {
            sdSpinner.setSelection(3);
            sdErr.setText("Current fs: " + adapter.getItem(1));
        } else {
            sdErr.setText("The current fs is not supported by this app.");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu, menu);
        return (super.onCreateOptionsMenu(menu));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.aboutMenu:
                startActivity(new Intent(this, About.class));
                //finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void saveExtension(View v) {
        ArrayList<String> items = new ArrayList<String>();

        Spinner systemSpinner = (Spinner) findViewById(R.id.system_spinner);
        Spinner cacheSpinner = (Spinner) findViewById(R.id.cache_spinner);
        Spinner dataSpinner = (Spinner) findViewById(R.id.data_spinner);
        Spinner sdSpinner = (Spinner) findViewById(R.id.sd_spinner);

        CheckBox systemCb = (CheckBox) findViewById(R.id.systemcheck);
        CheckBox cacheCb = (CheckBox) findViewById(R.id.cachecheck);
        CheckBox dataCb = (CheckBox) findViewById(R.id.datacheck);
        CheckBox sdCb = (CheckBox) findViewById(R.id.sdcheck);

        if (systemCb.isChecked()) {
            items.add("stl6 " + systemSpinner.getSelectedItem().toString() + "\n");
        }
        if (dataCb.isChecked()) {
            items.add("stl7 " + dataSpinner.getSelectedItem().toString() + "\n");
        }
        if (cacheCb.isChecked()) {
            items.add("stl8 " + cacheSpinner.getSelectedItem().toString() + "\n");
        }
        if (sdCb.isChecked()) {
            items.add("mmcblk0p2 " + sdSpinner.getSelectedItem().toString());
        }

        writefile write = new writefile(items, "/Android/data/g3mod", "fs.convert", true);

        final Intent main = new Intent(this, MainActivity.class);
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        try {
                            Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot now"});
                        } catch (IOException ex) {
                            //It doesn't work..
                        }
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        startActivity(main);
                        break;
                }
            }
        };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("fs.convert created. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
        builder.setCancelable(false);
    }
}
